<template>
  <el-dialog title="添加员工" :visible="showDialog" @close="close">
    <el-form ref="form" :rules="rules" label-width="120px" :model="formData">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" placeholder="请输入密码" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button type="text" size="small" @click="close">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '',
        email: '',
        password: ''
      },
      rules: {
        username: [{
          required: true, message: '姓名不能为空', trigger: 'blur'
        }],
        email: [{
          required: true, message: '邮箱不能为空', trigger: 'blur'
        }],
        password: [{
          required: true, message: '密码不能为空', trigger: 'blur'
        }]

      }
    }
  },
  methods: {
    // 提交新增员工
    async submit() {
      await this.$refs.form.validate()
      await addEmployee(this.formData)
      this.$emit('updateEve')
      this.close()
    },
    // 关闭窗口
    close() {
      this.$emit('update:showDialog', false)
      this.formData = {
        username: '',
        email: '',
        password: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style>

</style>
